package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.article.dtos.UserRelationDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserfollowMapper;
import com.heima.user.service.ApUserfollowService;
import com.heima.utils.common.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author rookie001
 * @creat 2023-06-21-10:32
 */
@Service
@Slf4j
public class ApUserfollowServiceImpl implements ApUserfollowService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ApUserFanMapper apUserFanMapper;
    @Autowired
    private ApUserfollowMapper apUserfollowMapper;
    @Autowired
    private ApUserMapper apUserMapper;

    //关注 取消关注
    @Override
    public ResponseResult userFollow(UserRelationDto dto) {
        //数据校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //获取用户id
        Integer id = AppThreadLocalUtils.getUser().getId();
        //判断用户是否登录
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        //设置用户回显key
        String keyUser = id + dto.getArticleId() + dto.getAuthorId() + "userlike";
        //设置作者回显key
        //String keyAuthor = dto.getAuthorId() + dto.getArticleId() + "author";
        //获取redis中数据
        String unlike = (String) redisTemplate.opsForValue().get(keyUser);
        if (unlike == null) {
            //若为空添加 到用户
            redisTemplate.opsForValue().set(keyUser, dto.getOperation() + "");
            log.info("点赞用户为 : {}", id);
            //关注后添加到  ap_user_follow
            //获取用户名
            ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getId, id));
            if (apUser!=null) {
                ApUserFollow apUserFollow = ApUserFollow.builder()
                        .userId(id)
                        .followId(dto.getAuthorId())
                        .followName(apUser.getName())
                        .level((short) 0)
                        .isNotice(true)
                        .createdTime(new Date())
                        .build();
                apUserfollowMapper.insert(apUserFollow);
                //关注后添加到 ap_user_fan
                ApUserFan apUserFan= ApUserFan.builder()
                        .userId(dto.getAuthorId())
                        .fansId(id)
                        .fansName(apUser.getName())
                        .level((short) 0)
                        .createdTime(new Date())
                        .isDisplay(true)
                        .isShieldLetter(true)
                        .isShieldComment(true)
                        .build();
                apUserFanMapper.insert(apUserFan);
            }
            //作者redis添加
            //redisTemplate.opsForValue().set(keyAuthor, id + "");
        } else {
            if (unlike.equals("0")) {
                //删除用户redis
                redisTemplate.delete(keyUser);
                log.info("取消点赞用户为 : {}", id);
                //删除作者redis
                //edisTemplate.delete(keyAuthor);
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
